<?php

namespace app\admin\controller\work_area;

use app\admin\service\work_area\BusinessService;
use app\common\controller\Backstage;
use app\common\model\work_area\WorkAreaUser;
use app\common\service\RedisQuantity;
use hg\apidoc\annotation as Apidoc;
use thans\jwt\facade\JWTAuth;

/**
 * @Apidoc\Title("工区人员管理")
 */
class User extends Backstage
{
    //中间件不验证的方法
    protected $middleware = [
        'app\\middleware\\Admin' => [
            'except' => ['info']
        ]
    ];

    /**
     * @Apidoc\Title("工区人员列表")
     * @Apidoc\Url("/admin/work_area.User/listApi")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("keyword", type="string", desc="关键字")
     * @Apidoc\Param("keyword_time", type="array", desc="数组0开始 1结束")
     * @Apidoc\Param("page", type="int", desc="第几页默认第1页")
     * @Apidoc\Param("page_size", type="int", desc="每页几条默认10条")
     * @Apidoc\Returned("data", type="array", desc="返回说明")
     * @Apidoc\Returned("total", type="int", desc="总条数")
     */
    public function listApi()
    {
        try {
            $param = $this->request->param();
            $res = BusinessService::workAreaUserList($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '成功', $res);
    }


    /**
     * @Apidoc\Title("劳务单位详情")
     * @Apidoc\Url("/admin/work_area.User/info")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="劳务单位id")
     * @Apidoc\Returned("data", type="object", desc="对象信息",
     *    @Apidoc\Returned("account", type="string", desc="账号"),
     *    @Apidoc\Returned("type", type="int", desc="1总部 2工区账号"),
     *    @Apidoc\Returned("work_area_id", type="int", desc="工区id"),
     *    @Apidoc\Returned("work_area_stand_id", type="int", desc="站点id"),
     *    @Apidoc\Returned("remarks", type="string", desc="备注"),
     * )
     */
    public function info()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            $res = BusinessService::workAreaUserInfo($param);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '获取成功', $res);
    }

    /**
     * @Apidoc\Title("添加,修改")
     * @Apidoc\Url("/admin/work_area.User/edit")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int", desc="修改传id")
     * @Apidoc\Param("account",type="string",require=true, desc="账号")
     * @Apidoc\Param("password", type="string",require=true, desc="密码")
     * @Apidoc\Param("password_confirm", type="string",require=true, desc="确认密码")
     * @Apidoc\Param("type", type="int",require=true, desc="1总部 2工区账号")
     * @Apidoc\Param("work_area_id", type="int",require=true, desc="工区id")
     * @Apidoc\Param("remarks", type="string", desc="备注")
     * @Apidoc\Returned("data", type="object", desc="返回说明")
     */
    public function edit()
    {
        try {
            $param = $this->request->param();
            $result = JWTAuth::auth();
            $admin_id = $result['admin_id'];
            $data = [
                'account|账号' => 'require|length:8,15',
                'password|密码' => 'require|length:8,15|confirm',
            ];
            //修改不填密码不验证
            if (isset($param['id']) && empty($param['password']) && empty($param['password_confirm'])) unset($data['password|密码']);
            Validate($data)->check($param);
            BusinessService::workAreaUserSave($param, $admin_id);
            if (!RedisQuantity::FrequencyLimit(md5(request()->controller() . request()->action() . $admin_id), 2)) {
                throw new \Exception('请求频繁,稍后再试');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }


    /**
     * @Apidoc\Title("删除")
     * @Apidoc\Url("/admin/work_area.User/del")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id",type="int",require=true, desc="id")
     * @Apidoc\Returned("data", type="object", desc="对象信息" )
     */
    public function del()
    {
        try {
            $param = $this->request->param();
            Validate([
                'id|系统错误' => 'require',
            ])->check($param);
            WorkAreaUser::destroy($param['id']);

        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->ajax(0, $e->getMessage());
        }
        return $this->ajax(1, '操作成功');
    }
}